package com.companion.lh.app

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.text.TextUtils
import android.widget.Toast
import com.companion.lh.app.dto.LoginDto
import com.companion.lh.companion_app.R
import com.companion.model.Response
import com.companion.service.LoginService
import com.companion.service.retrofit
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.android.synthetic.main.activity_password.*

class PasswordActivity : AppCompatActivity() {

    private val loginService = retrofit.create(LoginService::class.java)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_password)
        countdownVerifycode()
        loginBtn.setOnClickListener { login() }
        verifyCodeAgainBtn.setOnClickListener { getVerifyCodeAgain() }

    }

    private fun countdownVerifycode() {
        verifyCodeAgainBtn.isClickable = false
        for (i in 59..1) {
            verifyCodeAgainBtn.text = i.toString() + "s"
            Thread.sleep(1000)
        }
        verifyCodeAgainBtn.isClickable = true
    }

    private fun login() {
        if (isValidVerifyCode()) {
            var loginDto = LoginDto()
            loginDto.phone = intent.extras.getString("phone")
            loginDto.verifyCode = getVerifyCode()
            loginService.login(loginDto).subscribeOn(AndroidSchedulers.mainThread())
                    .subscribe { response: Response? ->
                        run {
                            if (response?.code == 1) {
                                Toast.makeText(this, "login success", Toast.LENGTH_SHORT)
                            } else {
                                Toast.makeText(this, response?.msg, Toast.LENGTH_SHORT)
                            }
                        }
                    }
        }
    }

    private fun isValidVerifyCode(): Boolean {
        return if (TextUtils.isEmpty(getVerifyCode()) || getVerifyCode()?.length!! < 6) {
            Toast.makeText(this, R.string.error_invalid_verifycode, Toast.LENGTH_SHORT)
            false
        } else {
            true
        }
    }

    private fun getVerifyCode(): String? {
        return codeEdit1.text.toString() + codeEdit2.text.toString() + codeEdit3.text.toString() + codeEdit4.text.toString() + codeEdit5.text.toString() + codeEdit6.text.toString()
    }

    private fun getVerifyCodeAgain() {
        val phone = intent.extras.getString("phone")
        loginService.getVerifyCode(phone).subscribeOn(AndroidSchedulers.mainThread())
                .subscribe { response: Response? ->
                    run {
                        if (response?.code == 1) {
                            Toast.makeText(this, R.string.info_send_verifycode_success, Toast.LENGTH_SHORT)
                        } else {
                            Toast.makeText(this, R.string.info_send_verifycode_failure, Toast.LENGTH_SHORT)
                        }
                    }
                }
    }
}
